import { action, observable } from 'mobx';
import { listCategory, listDict, saveDict, deleteDict, saveDictItem, listDictItem } from '../service';
import loading from '../../../components/Loading';
import { message } from 'antd';
import  '../../../components/ChangeTheme';

class AppStore {

  // 数据字典列表
  @observable dictListData = {
    dataSource: [],
    total: 0,
    loading: false,
    pageSize: 10,
    pageNo: 1,
  };

  // 分类
  @observable categoryList = [];

  // 数据字典弹出框参数
  @observable dictDialogData = {
    loading: false,
    visible: false,
    editData: {},
  };

  // 数据字典抽屉数据
  @observable dictItemDrawer = {
    dictItemList: [],
    visible: false,
    dict: {},
  };

  /**
   * 析构函数
   */
  constructor() {
    this.initData();
  }

  /**
   * 初始化页面，加载数据
   * @returns {Promise<void>}
   */
  async initData() {
    this.categoryList = await listCategory();
    this.initListDict();
  }

  /**
   * 获取数据字典列表
   * @returns {Promise<void>}
   */
  async initListDict() {
    let { pageNo, pageSize, categoryId, name } = this.dictListData;
    let data = await loading(listDict, this.dictListData)(pageNo, pageSize, categoryId, name);
    this.dictListData = { ...this.dictListData, dataSource: data.rows, total: data.total };
  }

  /**
   * 弹出数据字典对话框
   * @param params 新增或者编辑的数据字典
   */
  @action
  showDictDialog(params) {
    this.dictDialogData = { ...this.dictDialogData, visible: true, editData: params ? params : {} };
  }

  /**
   * 关闭数据字典对话框
   */
  @action
  closeDictDialog() {
    this.dictDialogData.visible = false;
  }

  /**
   * 保存数据字典
   * @param data 数据字典
   * @returns {Promise<void>}
   */
  @action
  async saveDict(data) {
    let result = await loading(saveDict, this.dictDialogData)(data);
    if (result.code == 0) {
      this.dictDialogData = { ...this.dictDialogData, visible: false };
      this.initListDict();
      message.info('保存数据字典成功');
    } else {
      message.warning(result.msg);
    }

  }

  /**
   * 根据条件查询数据字典列表
   * @param data
   */
  @action
  listDict(data) {
    this.dictListData = { ...this.dictListData, pageNo: 1, pageSize: 10, categoryId: data.categoryId, name: data.name };
    this.initListDict();
  }

  /**
   * 分页查询数据字典列表
   * @param pageNo 页码
   * @param pageSize 每页条数
   * @returns {Promise<void>}
   */
  @action
  async paginationListDict(pageNo, pageSize) {
    this.dictListData = { ...this.dictListData, pageNo: pageNo, pageSize: pageSize, loading: true };
    this.initListDict();
  }

  /**
   * 删除数据字典
   * @param record 数据字典
   * @returns {Promise<void>}
   */
  @action
  async deleteDict(record) {
    await loading(deleteDict, this.dictListData)(record.id);
    await this.initListDict();
  }

  /**
   * 显示数据字典内容抽屉
   * @param dictId 数据字典Id
   * @returns {Promise<void>}
   */
  @action
  async showDictItemDrawer(dict) {
    this.dictItemDrawer = {
      ...this.dictItemDrawer,
      visible: true,
      dict: dict,
      dictItemList: await listDictItem(dict.id),
    };
  }

  /**
   * 关闭数据字典内容抽屉
   */
  @action
  closeDictItemDrawer() {
    this.dictItemDrawer.visible = false;
  }

  /**
   * 新增数据字典内容
   */
  @action
  insertDictItem() {
    let dictItemList = this.dictItemDrawer.dictItemList;
    dictItemList.push({ dictId: this.dictItemDrawer.dictId });
    this.dictItemDrawer = { ...this.dictItemDrawer, dictItemList: dictItemList };
  }

  /**
   * 删除数据字典内容
   * @param index 行数
   */
  @action
  removeDictItem(index) {
    let dictItemList = this.dictItemDrawer.dictItemList;
    dictItemList.splice(index, 1);
    this.dictItemDrawer = { ...this.dictItemDrawer, dictItemList: dictItemList };
  }

  /**
   * 保存数据字典内容
   * @returns {Promise<void>}
   */
  @action
  async saveDictItem() {
    await loading(saveDictItem, this.dictItemDrawer)(this.dictItemDrawer.dict, this.dictItemDrawer.dictItemList);
    this.closeDictItemDrawer();
  }

  /**
   * 数据字典内容输入处理
   * @param col 列
   * @param value 值
   * @param index 行数
   */
  @action
  inputDictItem(col, value, index) {
    let dictItemList = this.dictItemDrawer.dictItemList;
    let record = this.dictItemDrawer.dictItemList[index];
    record[col] = value;
    dictItemList.splice(index, 1, record);
    this.dictItemDrawer = { ...this.dictItemDrawer, dictItemList: dictItemList };
  }
}

export default AppStore;
